package com.woniuxy.userapp.app.controller;



import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.commons.util.Result;
import com.woniuxy.userapp.client.FrontAccountClient;
import com.woniuxy.userapp.client.param.*;
import com.woniuxy.userapp.common.exception.Myexception.UserException;
import com.woniuxy.userapp.common.exception.code.UserExceptionCode;
import com.woniuxy.userapp.common.util.BindingResultUtil;
import com.woniuxy.userapp.infra.model.mapper.model.FrontAccount;
import com.woniuxy.userapp.service.FrontAccountService;
import com.woniuxy.userapp.service.dto.ChangeAccountMoneyDto;
import com.woniuxy.userapp.service.dto.UserDto;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * <p>
 *  前台账户
 * </p>
 *
 * @author 
 * @since 2022-08-03
 */
@RestController
public class FrontAccountController implements FrontAccountClient {

    @Resource
    private FrontAccountService frontAccountService;


    public Result<UserInfo> FloadUserByUsername(@RequestParam String userName){
        UserDto userDto = frontAccountService.loadUserByUsername(userName);
        UserInfo userInfo = BeanUtil.copyProperties(userDto, UserInfo.class);
        return Result.getInstance(UserInfo.class).setCode(200).setMsg("查询成功").setData(userInfo);
    }

    @GlobalTransactional(rollbackFor = Exception.class)
    public Result<FrontAccountInfo> loadUserByAccountId(Integer id){
        FrontAccount frontAccount = frontAccountService.getOne(new QueryWrapper<FrontAccount>().eq("id", id));
        if(frontAccount == null) throw new UserException(UserExceptionCode.ACCOUNT_NOT_EXISTS);
        FrontAccountInfo frontAccountInfo = BeanUtil.copyProperties(frontAccount, FrontAccountInfo.class);
        return Result.getInstance(FrontAccountInfo.class).setData(frontAccountInfo).setMsg("查询成功").setCode(200);
    }
//
    @GlobalTransactional(rollbackFor = Exception.class)
    public Result changeMoneyByAccountId(ChangeAccountMoneyInfo changeMoneyInfo){
        ChangeAccountMoneyDto changeMoneyDto = new ChangeAccountMoneyDto();
        BeanUtil.copyProperties(changeMoneyInfo, changeMoneyDto);
        frontAccountService.changeMoney(changeMoneyInfo);
        return Result.getInstance().setMsg("修改成功").setCode(200);
    }

    @GlobalTransactional(rollbackFor = Exception.class)
    public Result autoDeductMoney(AutoDeductMoneyInfo autoDeductMoneyInfo){
        frontAccountService.autoDeductMoney(autoDeductMoneyInfo);
        return Result.getInstance().setCode(200).setMsg("划钱成功");
    }

//    @GlobalTransactional(rollbackFor = Exception.class)
    @Override
    public Result<BigDecimal> findMoneyByAccountId(Integer accountId){
        FrontAccount frontAccount = frontAccountService.getOne(new QueryWrapper<FrontAccount>().eq("id", accountId));
        if(frontAccount ==null) throw new UserException(UserExceptionCode.ACCOUNT_NOT_EXISTS);
        return Result.getInstance(BigDecimal.class).setCode(200).setMsg("查询成功").setData(frontAccount.getMoney());
    }

    /**
     * 商家,教练提现
     * @param depositInfo
     * @return
     */
    @Override
    public Result deposit(DepositInfo depositInfo) {
        frontAccountService.deposit(depositInfo);
        return Result.getInstance().setCode(200).setMsg("提现成功");
    }

    /**
     * 注册
     * @param registerInfo
     * @return
     */
    @Override
    public Result register(RegisterInfo registerInfo) {
        frontAccountService.register(registerInfo);
        return Result.getInstance().setMsg("注册成功").setCode(200);
    }

    /**
     * 前台登录
     * @param loginInfo
     * @return
     * @throws HttpRequestMethodNotSupportedException
     */
    @Override
    public Result login(LoginInfo loginInfo) throws HttpRequestMethodNotSupportedException {
        Result login = frontAccountService.login(loginInfo.getUsername(), loginInfo.getPassword());
        return login;
    }

    @Override
    public Result recharge(RechargeInfo rechargeInfo) {
        frontAccountService.recharge(rechargeInfo);
        return Result.getInstance().setCode(200).setMsg("充值成功");

    }
}

